from tortoise import models, fields
from tortoise.contrib.pydantic import pydantic_model_creator

from passlib.hash import pbkdf2_sha256


class User(models.Model):
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=222, unique=True)
    password_hash = fields.CharField(max_length=222)
    date_join = fields.DatetimeField(auto_now_add=True)

    @classmethod
    async def get_user(cls, username):
        return cls.get(username=username)

    def verify_password(self, password):
        return pbkdf2_sha256.verify(password, self.password_hash)
    # class PydanticMeta:


userIn_pydantic = pydantic_model_creator(User, exclude=('date_join',))
userOut_pydantic = pydantic_model_creator(User, exclude=('password_hash',))
